﻿using System;
using JetBrains.Annotations;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Resolve;
using JetBrains.ReSharper.Psi.Xaml.Impl.Resolve;

namespace GammaJul.ReSharper.GammaXaml.Psi.Resolve {
	
	/// <summary>
	/// Symbol filter that filters on a type owner's type.
	/// </summary>
	public class TypeOwnerFilter : SimpleSymbolFilterWithErrorType {
		private readonly IType _type;

		public override bool Accepts(IDeclaredElement declaredElement, ISubstitution substitution) {
			if (_type.IsUnknown)
				return true;

			var typeOwner = declaredElement as ITypeOwner;
			return typeOwner != null && Equals(typeOwner.Type, _type);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TypeOwnerFilter"/> class.
		/// </summary>
		/// <param name="type">The type that a declared element must have to pass the filter.</param>
		public TypeOwnerFilter([NotNull] IType type)
			: base(XamlResolveErrorType.INVALID_TYPE) {
			if (type == null)
				throw new ArgumentNullException("type");
			_type = type;
		}
	}

}